package services.common;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import models.common.bean.ReceivablesPushCreditBean;
import models.common.entity.t_receivables_push;
import services.base.BaseService;

import common.utils.Factory;
import common.utils.PageBean;

import daos.common.ReceivablesPushDao;

/**
 * 账款推送关系的业务逻辑层
 * 
 * @author LiangQiongfeng
 * @version 2018-1-10 下午8:42:32
 */
public class ReceivablesPushService extends BaseService<t_receivables_push> {

	protected static ReceivablesPushDao receivablesPushDao = Factory
			.getDao(ReceivablesPushDao.class);

	protected static CompanyInfoService companyInfoService = Factory
			.getService(CompanyInfoService.class);

	protected static NoticeService noticeService = Factory
			.getService(NoticeService.class);

	/**
	 * 账款推送关系的的确立(新增或修改)
	 * 
	 * @param receivables_push
	 */
	public static boolean save(t_receivables_push receivables_push) {

		return receivablesPushDao.save(receivables_push);
	}

	/**
	 * 推送记录查询
	 * 
	 * @param time
	 *            推送时间
	 * @param lender_id
	 *            放款人id
	 * @param user_id
	 *            融通人id
	 * @param creditor_right_id
	 *            账款id
	 * @param status
	 *            状态 0 默认 1 推送中 2 已确认 3 退回'
	 * @param credit_limit_id
	 *            申请表id
	 * @return
	 */
	public static t_receivables_push findByCondistion(Date time,
			Long lender_id, Long user_id, Long creditor_right_id,
			Integer status, Integer credit_limit_id) {
		StringBuffer querySQL = new StringBuffer(
				"SELECT id,time,lender_id,user_id,creditor_right_id,status,credit_limit_id FROM t_receivables_push WHERE 1=1");
		Map<String, Object> condistion = new HashMap<String, Object>();
		if (time != null) {
			condistion.put("time", time);
			querySQL.append("AND time = :time");
		}
		if (lender_id != null && lender_id > 0) {
			condistion.put("lender_id", lender_id);
			querySQL.append("AND lender_id = :lender_id");
		}
		if (user_id != null && user_id > 0) {
			condistion.put("user_id", user_id);
			querySQL.append("AND user_id = :user_id");
		}
		if (creditor_right_id != null && creditor_right_id > 0) {
			condistion.put("creditor_right_id", creditor_right_id);
			querySQL.append("AND creditor_right_id = :creditor_right_id");
		}
		if (status != null) {
			condistion.put("status", status);
			querySQL.append("AND status = :status");
		}
		if (credit_limit_id != null && credit_limit_id > 0) {
			condistion.put("credit_limit_id", credit_limit_id);
			querySQL.append("AND credit_limit_id = :credit_limit_id");
		}
		return receivablesPushDao.findBySQL(querySQL.toString(), condistion);
	}

	/**
	 * 根据状态分页查询
	 * 
	 * @param currPage
	 * @param pageSize
	 * @param status
	 *            状态 0 默认 1 推送中 2 已确认 3 退回'
	 * @return
	 */
	public static PageBean<t_receivables_push> pageOfAdvertisementBack(
			int currPage, int pageSize, Integer status) {

		Map<String, Object> condition = new HashMap<String, Object>();
		StringBuffer countSQL = new StringBuffer(
				"SELECT COUNT(1) FROM t_receivables_push WHERE 1=1");
		StringBuffer querySQL = new StringBuffer(
				"SELECT id,time,lender_id,user_id,creditor_right_id,status,credit_limit_id FROM t_receivables_push WHERE 1=1");
		if (status != null) {
			condition.put("status", status);
			countSQL.append("AND status = :status");
			querySQL.append("AND status = :status");
		}

		return receivablesPushDao.pageOfBySQL(currPage, pageSize,
				countSQL.toString(), querySQL.toString(), condition);
	}

	/**
	 * 更新账款推送关系表状态信息
	 * 
	 * @param applyUserID
	 *            融通人ID
	 * @param receiverUserID
	 *            账款ID
	 * @param dirCreditStatus
	 *            目的推送状态
	 * @param srcCreditStatus
	 *            原推送状态
	 * @param userId
	 *            放款人ID
	 * @return
	 * @author fanghanyong
	 * @version 2018-1-16
	 */
	public int updateReceivablesPushStatus(long applyUserID,
			long receiverUserID, int dirCreditStatus, int srcCreditStatus,
			long userId) {

		return receivablesPushDao.updateReceivablesPushStatus(applyUserID,
				receiverUserID, dirCreditStatus, srcCreditStatus, userId);
	}

	/**
	 * 融通人-放款人绑定信息查询
	 * 
	 * @param currPage
	 * @param pageSize
	 * @param lender_id
	 *            放款人ID
	 * @param status
	 *            绑定状态
	 * @param startDate
	 *            开始时间
	 * @param lender_id
	 *            结束时间
	 * @return
	 * @author fanghanyong
	 * @createDate 2018年01月11日
	 */
	public PageBean<ReceivablesPushCreditBean> pageOfUserId(int currPage,
			int pageSize, long userId, int status, String startDate,
			String endDate, int exports) {
		return receivablesPushDao.pageOfUserId(currPage, pageSize, userId,
				status, startDate, endDate, exports);
	}

	/**
	 * 融通人-放款人绑定信息查询总额度
	 * 
	 * @param lender_id
	 * @param status
	 *            绑定状态
	 * @param startDate
	 *            开始时间
	 * @param lender_id
	 *            结束时间
	 * @return
	 * @author fanghanyong
	 * @createDate 2018年01月11日
	 */
	public Map<String, Object> totalAmountLeader(long userId, int status,
			String startDate, String endDate) {
		return receivablesPushDao.totalAmountLeader(userId, status, startDate,
				endDate);
	}

	/**
	 * 查询推送表数据
	 * @param creditId 授信申请表id
	 * @param userId  放款人id
	 * @param applyUserId 授信申请人id
	 * @return
	 * @author Liangqiongfeng
	 *@version 2018-3-22 
	 */
	public t_receivables_push findTRPBySql(long creditId, long userId,
			long applyUserId) {
		
		return receivablesPushDao.findTRPBySql( creditId,  userId, applyUserId);
	}
}
